package com.sctvcloud.bazhong.ui.utils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.widget.IntervalDecoration;

/**
 * Created by Administrator on 2017/7/13 0013.
 */

public class UiListMarginUtils {
    private IntervalDecoration decoration;

    public UiListMarginUtils setMargin(Context context, RecyclerView recycleriew, int horMargin, int verMargin) {
        if (decoration != null) {
            int hor = context.getResources().getDimensionPixelOffset(horMargin);
            int ver = context.getResources().getDimensionPixelOffset(verMargin);
            decoration.setInterval(hor, ver);
            setFilterRightMagin(hor, recycleriew);
        } else {
            int hor = context.getResources().getDimensionPixelOffset(verMargin);
            int ver = context.getResources().getDimensionPixelOffset(verMargin);
            decoration = new IntervalDecoration(hor, ver).setIgnorFarRight(false);
            setFilterRightMagin(hor, recycleriew);
            recycleriew.addItemDecoration(decoration);
        }
        return this;
    }

    public UiListMarginUtils setHorMargin(Context context, RecyclerView recyclerView, int horMargin) {
        if (decoration != null) {
            int hor = context.getResources().getDimensionPixelOffset(horMargin);
            decoration.setInterval(hor, 0);
            setFilterRightMagin(hor, recyclerView);
        } else {
            int hor = context.getResources().getDimensionPixelOffset(horMargin);
            decoration = new IntervalDecoration(hor, 0);
            setFilterRightMagin(hor, recyclerView);
            recyclerView.addItemDecoration(decoration);
        }
        return this;
    }
    public UiListMarginUtils setVerticalMargin(Context context, RecyclerView recyclerView, int vertical) {
        if (decoration != null) {
            int ver = context.getResources().getDimensionPixelOffset(vertical);
            decoration.setInterval(0, ver);
            setFilterRightMagin(0, recyclerView);
        } else {
            int ver = context.getResources().getDimensionPixelOffset(vertical);
            decoration = new IntervalDecoration(0, ver);
            setFilterRightMagin(0, recyclerView);
            recyclerView.addItemDecoration(decoration);
        }
        return this;
    }
    private void setFilterRightMagin(int horDecoration, RecyclerView recycleriew) {
        ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) recycleriew.getLayoutParams();

        mlp.rightMargin = mlp.leftMargin - horDecoration;
        if (mlp.rightMargin < 0) {
            mlp.rightMargin = 0;
        }
        recycleriew.setLayoutParams(mlp);
    }
}
